package com.jian.mygame;

import org.jbox2d.common.Vec2;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.drawable.Drawable;

public class MyPainter extends Object{
	
	static public void drawLine (Canvas canvas,float xSrc,float ySrc, float xDst ,float yDst ) {
		//Save Current Canvas
		canvas.save();
		
		Paint mPaint = new Paint();
		mPaint.setAntiAlias(true);
		mPaint.setColor(Color.RED);
		canvas.drawLine(xSrc, ySrc, xDst, yDst, mPaint);
		
		//Restore for the next object to be drawn
		canvas.restore();
	}
	
	static public void drawText(Canvas canvas, float x, float y, int fgColor, int bgColor, String str ){
		//Save Current Canvas
		canvas.save();
		
		Paint mPaint = new Paint();
		mPaint.setAntiAlias(true);
		mPaint.setTextSize(24);
		mPaint.setColor(fgColor);
		canvas.drawText(str, x, y, mPaint);
		mPaint.setColor(bgColor);
		mPaint.setStyle(Paint.Style.STROKE);
		//canvas.drawRect(210, 5, 100, 40, mPaint);
		
		//Restore for the next object to be drawn
		canvas.restore();
	}	
	
	static public void drawBox(Canvas canvas, float x,float y, float half_width,float half_height, 
			            float angle, int colorIdx){
		Paint mPaint = new Paint();
		float degrees = MyUtil.angleToDegree(angle );
		
		//Save Current Canvas
		canvas.save();
		
		//Rotate
		canvas.rotate((float)degrees, x*MyWorld.RATE, y*MyWorld.RATE);
		
		//Draw rectangle
		mPaint.setAntiAlias(true);
		mPaint.setColor(colorIdx);
		canvas.drawRect(x-half_width, y-half_height, x+half_width, y+half_height, mPaint);
		
		//Restore for the next object to be drawn
		canvas.restore();
	}

	static public void drawBox(Canvas canvas, float x,float y, float angle, Drawable image){
		float degrees = MyUtil.angleToDegree(angle );
		
		//Save Current Canvas
		canvas.save();
		
		//Rotate
		canvas.rotate((float)degrees, x*MyWorld.RATE, y*MyWorld.RATE);
		
		//Draw rectangle
		image.setBounds((int)(x - image.getIntrinsicWidth()/2),
				        (int)(y - image.getIntrinsicHeight()/2),
				        (int)(x + image.getIntrinsicWidth()/2), 
				        (int)(y + image.getIntrinsicHeight()/2));
		image.draw(canvas);
		
		//Restore for the next object to be drawn
		canvas.restore();
	}
	
	static public void drawCircle(Canvas canvas,float x,float y, float radius, float angle, int colorIdx, Drawable image){
		float degrees = MyUtil.angleToDegree(angle );
		
		//Save Current Canvas
		canvas.save();
		
		//Rotate
		canvas.rotate((float)degrees, x*MyWorld.RATE, y*MyWorld.RATE);
		
		if ( image == null) {
			Paint mPaint = new Paint();
			mPaint.setAntiAlias(true);
			mPaint.setColor(colorIdx);
			canvas.drawCircle(x, y, radius, mPaint);
		}else {
			image.setBounds((int)(x - image.getIntrinsicWidth()/2),
			        (int)(y - image.getIntrinsicHeight()/2),
			        (int)(x + image.getIntrinsicWidth()/2), 
			        (int)(y + image.getIntrinsicHeight()/2));
			image.draw(canvas);
		}
		
		//Restore for the next object to be drawn
		canvas.restore();
	}
	
	static public void drawPolygon(Canvas canvas, float xPos,float yPos, Vec2 vec[], float angle, int colorIdx){
		Paint mPaint = new Paint();
		Path mPath = new Path();
		float degrees = MyUtil.angleToDegree(angle );
		float x,y;
		
		//Save Current Canvas
		canvas.save();
		
		//Rotate
		canvas.rotate((float)degrees, xPos*MyWorld.RATE, yPos*MyWorld.RATE);
		
		//Draw
		mPaint.setAntiAlias(true);
		mPaint.setColor(colorIdx);
		
		//mPath.moveTo((x+vec[0].x)*MyWorld.RATE, (y+vec[0].y)*MyWorld.RATE);
		for( int i=0; i< vec.length; i++ ) {
			Vec2 mvec = vec[i];
			
			x = (xPos+mvec.x)*MyWorld.RATE;
			y = (yPos+mvec.y)*MyWorld.RATE;
			//Rotate vertex
			//if( angle != 0) {
			//	Log.d("GameHandler","x: "+xPos+" /y: "+yPos+" / angle: " + angle);
			//	x = (float) (Math.cos(angle) * x - Math.sin(angle) * y);
            //    y = (float) (Math.sin(angle) * x + Math.cos(angle) * y);
			//}
			if(i==0) {
				mPath.moveTo(x, y);
			} else {
				mPath.lineTo(x, y);
			}
		}
		canvas.drawPath(mPath, mPaint);

		
		//Restore for the next object to be drawn
		canvas.restore();
		
	}
	
	/*
	private void DrawContact( Canvas canvas, float x, float y ) {
		//float degrees = MyUtil.angleToDegree(angle );
		
		//Save Current Canvas
		canvas.save();
		
		//Rotate
		//canvas.rotate((float)degrees, x, y);
		
		Paint mPaint = new Paint();
		mPaint.setAntiAlias(true);
		mPaint.setColor(Color.BLUE);
		canvas.drawCircle(x, y, 10, mPaint);
		
		//Restore for the next object to be drawn
		canvas.restore();
	}
	*/
}
